package com.ipan.builder.entity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;

import com.ipan.builder.BuilderCfg;
import com.ipan.builder.ext.entity.DefEntityCfg;
import com.ipan.builder.ext.entity.annotation.DefTable;
import com.ipan.builder.util.BdClassLoaderUtils;
import com.ipan.builder.util.BdStringUtils;

/**
 * 操作实体类工具类
 * 
 * @author iPan
 * @version 2013-12-16
 */
public class EntityOpUtils {

	/**
	 * 加载所有JpaEntityCfg上下文
	 */
	public static Map<String, JpaEntityCfg> loadJpaEntityCfgContext() {
		Map<String, JpaEntityCfg> context = new HashMap<String, JpaEntityCfg>();
		String path = BuilderCfg.getInstance().getEntityClassPath().replace(".", "/");
		File dir = new File(BdClassLoaderUtils.getResource(path, EntityOpUtils.class).getFile());
		loadAllEntityClass(context, dir);
		return context;
	}
	
	/**
	 * 加载指定目录的JpaEntityCfg上下文
	 */
	public static Map<String, JpaEntityCfg> loadJpaEntityCfgContext(File dir) {
		Map<String, JpaEntityCfg> context = new HashMap<String, JpaEntityCfg>();
		loadAllEntityClass(context, dir);
		return context;
	}
	
	/**
	 * 加载所有JpaEntityCfg集合
	 */
	public static List<JpaEntityCfg> loadJpaEntityCfgList() {
		List<JpaEntityCfg> list = new ArrayList<JpaEntityCfg>(loadJpaEntityCfgContext().values());
		return list;
	}
	
	/**
	 * 加载指定目录的JpaEntityCfg集合
	 */
	public static List<JpaEntityCfg> loadJpaEntityCfgList(File dir) {
		List<JpaEntityCfg> list = new ArrayList<JpaEntityCfg>(loadJpaEntityCfgContext(dir).values());
		return list;
	}
	
	// 递归遍历指定目录
	private static void loadAllEntityClass(Map<String, JpaEntityCfg> context, File dir) {
		File[] files = dir.listFiles(); // 目录和文件
		for (File f : files) {
			if (f.isDirectory()) {
				loadAllEntityClass(context, f);
			} else {
				String fName = f.getName();
				if (fName.endsWith(".class")) { // 是Java可执行文件
					try {
						String classpathToPath = BuilderCfg.getInstance().getEntityClassPath().replace(".", "\\");
						String cpath = BdStringUtils.substringBeforeLast(f.getAbsolutePath(), ".class");
						cpath = cpath.substring(cpath.indexOf(classpathToPath));
						cpath = cpath.replace("\\", "."); // 获取classpath
						Class<?> classObj = BdClassLoaderUtils.loadClass(cpath, DefaultEntityOp.class);
						if (classObj.getAnnotation(Entity.class) != null) { // 是JPA实体
							context.put(classObj.getName(), new JpaEntityCfg(classObj));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	/**
	 * 加载所有DefEntityCfg上下文
	 */
	public static Map<String, DefEntityCfg> loadDefEntityCfgContext() {
		Map<String, DefEntityCfg> context = new HashMap<String, DefEntityCfg>();
		String path = BuilderCfg.getInstance().getEntityClassPath().replace(".", "/");
		File dir = new File(BdClassLoaderUtils.getResource(path, EntityOpUtils.class).getFile());
		loadAllDefEntityClass(context, dir);
		return context;
	}
	
	/**
	 * 加载指定目录的DefEntityCfg上下文
	 */
	public static Map<String, DefEntityCfg> loadDefEntityCfgContext(File dir) {
		Map<String, DefEntityCfg> context = new HashMap<String, DefEntityCfg>();
		loadAllDefEntityClass(context, dir);
		return context;
	}
	
	/**
	 * 加载所有DefEntityCfg集合
	 */
	public static List<DefEntityCfg> loadDefEntityCfgList() {
		List<DefEntityCfg> list = new ArrayList<DefEntityCfg>(loadDefEntityCfgContext().values());
		return list;
	}
	
	/**
	 * 加载指定目录的DefEntityCfg集合
	 */
	public static List<DefEntityCfg> loadDefEntityCfgList(File dir) {
		List<DefEntityCfg> list = new ArrayList<DefEntityCfg>(loadDefEntityCfgContext(dir).values());
		return list;
	}
	
	// 递归遍历指定目录
	private static void loadAllDefEntityClass(Map<String, DefEntityCfg> context, File dir) {
		File[] files = dir.listFiles(); // 目录和文件
		for (File f : files) {
			if (f.isDirectory()) {
				loadAllDefEntityClass(context, f);
			} else {
				String fName = f.getName();
				if (fName.endsWith(".class")) { // 是Java可执行文件
					try {
						String classpathToPath = BuilderCfg.getInstance().getEntityClassPath().replace(".", "\\");
						String cpath = BdStringUtils.substringBeforeLast(f.getAbsolutePath(), ".class");
						cpath = cpath.substring(cpath.indexOf(classpathToPath));
						cpath = cpath.replace("\\", "."); // 获取classpath
						Class<?> classObj = BdClassLoaderUtils.loadClass(cpath, DefaultEntityOp.class);
						if (classObj.getAnnotation(DefTable.class) != null) { // 是自定义实体
							context.put(classObj.getName(), new DefEntityCfg(classObj));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
}
